package com.five.monkey.feign.es.controller;

import com.five.monkey.feign.es.service.GraphElasticsearchService;
import com.five.monkey.vo.CommonVo;
import com.five.monkey.vo.GraphVo;
import com.five.monkey.vo.PageVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author 狻猊
 * @date 2021/8/10 下午2:08
 */
@RestController
@RequestMapping("/es/graph")
public class ESGraphController {

    @Autowired
    private GraphElasticsearchService graphElasticsearchService;

    @PostMapping("/save")
    public CommonVo<GraphVo> saveToElasticsearch(@RequestBody GraphVo graphVo) {
        graphVo = graphElasticsearchService.save(graphVo);
        return CommonVo.ok(graphVo);
    }

    @GetMapping("/find/by/{id}")
    public CommonVo<GraphVo> findByIdFromElasticsearch(@PathVariable("id") Long id) {
        GraphVo graphVo = graphElasticsearchService.findById(id);
        return CommonVo.ok(graphVo);
    }

    @DeleteMapping("/delete/by/{id}")
    public CommonVo<Boolean> deleteByIdFromElasticsearch(@PathVariable("id") Long id) {
        boolean flag = graphElasticsearchService.deleteById(id);
        return CommonVo.ok(flag);
    }

    @GetMapping("/find/all")
    public CommonVo<List<GraphVo>> findAllFromElasticsearch() {
        List<GraphVo> voList = graphElasticsearchService.findAll();
        return CommonVo.ok(voList);
    }

    @GetMapping("/page/by/{status}")
    public CommonVo<PageVo<GraphVo>> pageByStatusFromElasticsearch(@PathVariable("status") Integer status, @RequestParam(value = "page") int page,
                                                                  @RequestParam(value = "size", required = false, defaultValue = "10") int size) {
        PageVo<GraphVo> userVoPage = graphElasticsearchService.pageByStatus(status, page, size, null);
        return CommonVo.ok(userVoPage);
    }

    @GetMapping("/list/by/{status}")
    public CommonVo<List<GraphVo>> listByStatusFromElasticsearch(@PathVariable("status") Integer status) {
        List<GraphVo> userVoPage = graphElasticsearchService.findByStatus(status);
        return CommonVo.ok(userVoPage);
    }
}
